// 创建一个需要混入的对象 
export const mixinObj = {
	data() {
		return {
			msg: '我是mixin中的data数据',
		}
	},
	created() {
		// this.hello();
	},
	methods: {
		// hello() {
		// 	console.log('mixinTest1');
		// }
		// click(){
		// 	alert('我是mixin中的click方法')
		// },
		async checkEditPermission() {
			const res = await this.$http({
				method: 'get',
				url: this.$api.checkEditPermission,
				data: {
					id: this.userId,
					family_tree_id: this.treeId
				}
			});
			if (res.code && res.code == 200) {
				this.isHasEditPermission = res.data.edit_permission;
			}
		},


		//判断浏览器
		checkBrowser() {
			let status = navigator.userAgent.toLowerCase();
			if (status.match(/MicroMessenger/i) == "micromessenger") {
				return 'weChat'
			} else {
				return 'web'
			}
		},

		//检查手机系统
		checkSystem() {
			let mobileSystem = uni.getSystemInfoSync().platform;
			// console.log(mobileSystem); //andriod or ios
			return mobileSystem;
		},

		setStorageKey(k, v) {
			uni.setStorageSync(k, v);
		},



		// 获取分享的信息接口
		reqShare(obj) {
			return new Promise(async (resolve, reject) => {
				const res = await this.$http({
					method: 'get',
					url: this.$api.share,
					data: {
						type: obj.type
					}
				});
				if (res.code && res.code == 200) {
					resolve(res);
				}
			})

		},
		
		formatToastContent(resData) {
				//收自己的菜
				if (resData.earn_integral > 0) {
					if (resData.plunder_integral > 0 && resData.plunder_diamond > 0) {
						if(this.$common._empty(resData.word)){
							return `果实已成熟，其中被好友偷取<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.plunder_diamond}；最终收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''}`
						}else{
							return `果实已成熟，其中被好友偷取<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.plunder_diamond}；最终收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''},幸运的你额外获得了<span style="color:yellow;">“${resData.word}”</span>字`
						}
					}
					if (resData.plunder_integral > 0 && resData.plunder_diamond == 0) {
						if(this.$common._empty(resData.word)){
							return `果实已成熟，其中被好友偷取<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}；最终收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''}`
						}else{
							return `果实已成熟，其中被好友偷取<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}；最终收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''},幸运的你额外获得了<span style="color:yellow;">“${resData.word}”</span>字`
						}
					}
					if (resData.plunder_integral == 0 && resData.plunder_diamond == 0) {
						if(this.$common._empty(resData.word)){
							return `果实完美成熟，恭喜您收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''}`
						}else{
							return `果实完美成熟，恭喜您收获<img class="rich-img" src="/static/share-jifen.png" />${resData.earn_integral}${resData.earn_diamond > 0 ? `，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.earn_diamond}` : ''},幸运的你额外获得了<span style="color:yellow;">“${resData.word}”</span>字`
						}
					}
				}
				//偷别人菜
				else if (resData.earn_integral == 0) {
					if (resData.plunder_integral > 0 && resData.plunder_diamond > 0) {
						if(this.$common._empty(resData.word)){
							return `恭喜您，以迅雷掩耳之势偷取好友<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.plunder_diamond}`
						}else{
							return `恭喜您，以迅雷掩耳之势偷取好友<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}，<img class="rich-img" src="/static/big_zuanshi.png" />${resData.plunder_diamond},幸运的你额外获得了<span style="color:yellow;">“${resData.word}”</span>字`
						}
					}
					if (resData.plunder_integral > 0 && resData.plunder_diamond == 0) {
						if(this.$common._empty(resData.word)){
							return `恭喜您，以迅雷掩耳之势偷取好友<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral}`
						}else{
							return `恭喜您，以迅雷掩耳之势偷取好友<img class="rich-img" src="/static/share-jifen.png" />${resData.plunder_integral},幸运的你额外获得了<span style="color:yellow;">“${resData.word}”</span>字`
						}
					}
				}
		},


		formatGoodsClass(code) {
			switch (code) {
				case 'huluobo':
					return "huluobo";
					break;
				case 'dasuan':
					return "dasuan";
					break;
				case 'lajiao':
					return "lajiao";
					break;
				case 'qiezi':
					return "qiezi";
					break;
				case 'tudou':
					return "tudou";
					break;
				case 'chuji':
					return "paichu1";
					break;
				case 'zhongji':
					return "paichu2";
					break;
				case 'gaoji':
					return "paichu3";
					break;
			}
		},

		//原生app 返回方法
		bridgeJs() {
			if (uni.getSystemInfoSync().platform == 'android') {
				try {
					setTimeout(() => {
						window.android.backUp();
					}, 0)
				} catch (e) {
					// alert('返回失败android');
				}

			} else {
				try {
					window.webkit.messageHandlers.backUp.postMessage('1'); //postMessage 必须有值 空字符串也不行；
				} catch (e) {
					// alert('返回失败ios');
				}
			}
		},


		//公共返回上一级 
		navBack() {
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			if (this.entryType == 1) { //app进入
				// alert('entryType == 1')
				this.bridgeJs();
			} else {
				if (pages.length > 1) {

					uni.navigateBack({
						delta: 1,
					});

				} else {
					if (uni.getSystemInfoSync().platform == 'android') {

						try {
							setTimeout(() => {
								window.android.backUp();
							}, 0)
						} catch (e) {
							alert('返回失败android');
						}
					} else {

						try {
							window.webkit.messageHandlers.backUp.postMessage('1'); //postMessage 必须有值 空字符串也不行；
						} catch (e) {
							alert('返回失败ios');
						}
					}
				}
			}
		},


		async getChance(getType) {
			return new Promise(async (resolve, reject) => {
				const res = await this.$http({
					method: 'get',
					url: this.$api.getChance,
					data: {
						type: getType
					}
				});
				if (res.code && res.code == 200) {
					resolve(res);
				}
			})
		},


		getChanceList() {
			return new Promise(async (resolve, reject) => {
				const res = await this.$http({
					method: 'get',
					url: this.$api.getChanceList
				});
				if (res.code && res.code == 200) {
					resolve(res);
				}
			})
		},


		//调用接口通知后端已分享，记录次数
		async wxShareCallback(activeFlag) {
			return new Promise(async (resolve, reject) => {
				const res = await this.$http({
					method: 'get',
					url: this.$api.wxShareCallback,
					data: {
						type: activeFlag
					}
				});
				if (res.code && res.code == 200) {
					resolve(res);
				}

			})

		},

		async share(shareData) {
			const res = await this.reqShare(shareData);
			// console.log(res);
			if (res.code && res.code == 200) {
				let options = res.data;
				Object.assign(options, {
					flag: shareData.type
				})
				if (uni.getSystemInfoSync().platform == 'android') {
					window.android.wxShare(options.image, options.url, options.title, options.describe, options
						.flag);
				} else {
					// options.flag = String(options.flag);
					try {
						window.webkit.messageHandlers.wxShare.postMessage(options); //postMessage 必须有值 空字符串也不行；
					} catch (e) {
						alert('返回失败ios');
					}
				}

			}
		},



		

		openApp(url) { 
			if (uni.getSystemInfoSync().platform == 'android') {
				//安卓 scheme协议 唤起app  
				window.location.href = 'homechats://main';

				//未唤起执行下载操作
				setTimeout(function() {
					let hidden = window.document.hidden || window.document.mozHidden || window.document
						.msHidden || window.document.webkitHidden
					if (typeof hidden == "undefined" || hidden == false) {
						//安卓下载地址
						window.location.href = url;
					}
				}, 2500);
			} else {
				//ios scheme协议  唤起app
				window.location.href = 'homechats://main';
				setTimeout(function() {
					let hidden = window.document.hidden || window.document.mozHidden || window.document
						.msHidden || window.document.webkitHidden
					if (typeof hidden == "undefined" || hidden == false) {
						//App store下载地址
						window.location.href = url;
					}
				}, 2500);
				// return false;
			}
		},
	},
}
